package com.sinodata.bsm.cicp.collector.informix;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class IfxSqlCollector extends AbstractJDBCCollector {

    private static Logger logger = Logger.getLogger(IfxSqlCollector.class);

    /**
     * 
     */
    public IfxSqlCollector() {
    }

    public List<ResPropertyValue> collect(String ip) {

        String dbtype = this.paramMap.get("DBType");
        String dbname = this.paramMap.get("DBName");
        String sql = this.paramMap.get("SQL");
        String iip = this.paramMap.get("DBIP");

        String[] propMetas = getPropMeta();

        if (iip != null && !"".equals(iip))
            ip = iip;

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            ResultSet rs = this.getCachedResultSet(dbtype, ip, dbname, sql);
            ResultSetMetaData md = rs.getMetaData();
            if (md.getColumnCount() == propMetas.length) {
                while (rs.next()) {
                    ResPropertyValue propertyValueX = new ResPropertyValue();
                    ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                    for (int i = 0; i < propValues.length; i++) {
                        propValues[i] = ResPropertyValue.createPropValue(trans(rs.getObject(i + 1)), propMetas[i]);
                    }
                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                }
            }
        } catch (Exception e) {
            logger.error(e);
        }
        return values;
    }

    private static String trans(Object v) throws CollectorException {
        if (v == null)
            return null;
        String vs = v.toString();
        //0x表示16进制
        if (vs.startsWith("0x")) {
            try {
                vs = Integer.parseInt(vs.substring(2), 16) + "";
            } catch (Exception e) {
                logger.error("convert 16 binary (" + vs + ")to 16 binary wrong", e);
                throw new CollectorException(e);
            }
        }
        if (NumberUtils.isNumber(vs)) {
            if (NumberUtils.isDigits(vs)) {
                vs = Long.parseLong(vs) + "";
            } else {
                vs = Math.round((Double.parseDouble(vs.toString())) * 100) / 100.00 + "";
            }
        }
        return vs;
    }

    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName", "SQL", "FieldNum", "DBIP" };
    }

    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("FieldNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

    /**
     * 最多只支持两位
     * 
     * @param number
     * @return
     */
    private static String toChineseString(int number) {
        if (number == 0) {
            return "零";
        }
        String chineseString = "";
        int n = number / 10;
        if (n > 0) {
            if (n > 1) {
                chineseString += toChineseNumber(n);
            }
            chineseString += "十";
        }
        n = number % 10;
        if (n > 0) {
            chineseString += toChineseNumber(n);
        }
        return chineseString;
    }

    private static String toChineseNumber(int number) {
        String[] chineseNumbers = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        return chineseNumbers[number];
    }

}
